

%%
img1 = imread('图片1.png');
img2 = imread('图片2.png');

% 灰度图像
img1 = rgb2gray(img1);
img2 = rgb2gray(img2);

% 创建均值滤波器
h = fspecial('average', [2 2]);

% 对图像进行滤波
img1 = imfilter(img1, h);
img2 = imfilter(img2, h);

% 检测边缘
BW1 = edge(img1,'canny');
BW2 = edge(img2,'canny');

% img1 = imbinarize(img1, 0.4);
% img2 = imbinarize(img2, 0.4);

% [im_occ1, im_occ2, im_occ2_rot, R1, T1, D1, R2, T2, D2, R2_rot, T2_rot, D2_rot, delta_ang] = adjust_radon(img1, img2);

 [H1, T1, D1] = hough(BW1, 'Theta', -90:89);
 [H2, T2, D2] = hough(BW2, 'Theta', -90:89);
 
figure;
imshow(BW1);
title('Image 1');

figure;
imshow(BW2);
title('Image 2');
 
%  
% figure; imshow(img1, [-1 1]);
% set(gcf,'name','map_1','numbertitle','off');
% title('map_1', 'FontSize',12);
% %xlim([180 640]); ylim([160 480]);
% 
% figure; imshow(img2, [-1 1]);
% set(gcf,'name','map_2','numbertitle','off');
% title('map_2', 'FontSize',12);
% % %xlim([180 640]); ylim([160 480]);

peaks1 = [];
peaks2 = [];

% for Andrew's maps
max_no       = 10;
threshold    = 33;  % 34
d_gate       = 80;  % distance gate
v_gate       = 20;  % value gate

% H1 = -R1;

%% 计算并显示霍夫峰值
%peaks1 = GetRadonPeaks(H1, 100);
% peaks = houghpeaks(H,numpeaks) 定位由 hough 函数生成的 Hough 变换矩阵 H 中
% 的峰值。numpeaks 指定要识别的最大峰值个数。该函数返回 peaks，即一个保留峰值的行和列坐标的矩阵。
% peaks = houghpeaks(H,numpeaks,Name,Value) 使用名称-值对组参数控制运算的各个方面。
% peaks：找到的峰值的行和列坐标，但是我们这个是*3的一个向量
peaks1 = houghpeaks(H1, max_no, 'Threshold', threshold);


% peaks1 = houghpeaks(H1, 10);
% figure;imagesc(peaks1);
% set(gcf,'name','peaks_1','numbertitle','off');
% title('peaks_1', 'FontSize',12);
figure;
imshow(H1,[],'XData',T1,'YData',D1,'InitialMagnification','fit');
xlabel('\theta'), ylabel('\rho');
axis on, axis normal, hold on;
title('Hough image of map_1')
plot(T1(peaks1(:,2)),D1(peaks1(:,1)),'s','color','white');
set(gcf,'name','H-peaks1','numbertitle','off'); 
colormap(jet);

displayHoughspectrum(peaks1, T1);

% H2  = -R2;
peaks2 = houghpeaks(H2, max_no, 'Threshold', threshold);

figure;
imshow(H2,[],'XData',T2,'YData',D2,'InitialMagnification','fit');
xlabel('\theta'), ylabel('\rho');
title('Hough image of map_2')
axis on, axis normal, hold on;
plot(T2(peaks2(:,2)),D2(peaks2(:,1)),'s','color','white');
set(gcf,'name','H-peaks2','numbertitle','off'); 
colormap(jet);

displayHoughspectrum(peaks2, T2);